// pages/xianguapaiyouli/xianguapaiyouli.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    USER_ASSETS_IDCARD: '',
    USER_ASSETS_NAME: '',
    EMPLOYEE_ID: '',
    POST_ADD: '',
    POST_QX: '',
    wangdian: '',
    shangwang: '',
    sbwngdian: true,
    wangdiany: true,
    flag: true, //设置节流
    USER_IDCARD: '',
    jiami: '',
    OPEN_ID: '',
    session_key: '',
  },






  idcardblur: function (e) {
    let idcard = e.detail.value;
    let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;

    if (reg.test(idcard) == false) {
      this.setData({
        USER_IDCARD: ''
      })
      wx.showToast({
        title: '身份证号码有误',
        icon: 'none',
        duration: 3000
      })



    } else {
      this.setData({
        USER_IDCARD: idcard
      })


    }

  },










  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })
    } else if (this.data.mobile.length < 11) {
      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },



  wangdianshagn() {

    if (this.data.POST_ADD == '') {
      wx.showToast({
        title: '请先选择县区！',
        icon: 'none',
        duration: 1500
      })
    } else {

      this.setData({
        sbwngdian: false
      })
    }



  },
  wangshang(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.L4name);
    console.log(e.currentTarget.dataset.item.EMPLOYEE_ID);

    this.setData({
      POST_QX: e.currentTarget.dataset.item.L4name,
      sbwngdian: true,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,


    })
  },



  //网点选择
  wangji() {
    this.setData({
      wangdiany: false
    })
  },

  xwang(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.wangname);

    this.setData({

      shangwang: e.currentTarget.dataset.item.wd,
      POST_ADD: e.currentTarget.dataset.item.L3name,
      wangdiany: true,
      L3code: e.currentTarget.dataset.item.L3code,
      POST_QX: '',


    })
    // this.regionwangdian()
  },



  region() {
    wx.request({
      url: app.globalData.interface+'/xianPost/API_6059_xianGuaPaiYouLiEmployee',
      method: 'POST',
      data: {
        // L2code: 61012777,
        OPEN_ID: app.globalData.openid,
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami,

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          wangdian: res.data.data,
        })

      }
    })
  },



  regionwangdian() {
    wx.request({
      url: app.globalData.interface+'/ankangdabiao/API_5517_userQueryWangDian',
      method: 'POST',
      data: {
        L3code: this.data.L3code,
        OPEN_ID: this.data.openid,
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami,

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          shangwang: res.data.data,
        })

      }
    })
  },


  baoming() {

    if (this.data.flag) {
      this.data.flag = false



      if (this.data.USER_ASSETS_NAME == '' || this.data.USER_ASSETS_IDCARD == '' || this.data.POST_QX == '') {
        wx.showToast({
          title: '请完善报名信息',
          icon: 'none',
          duration: 2000
        })
      }else{

      wx.request({
        url: app.globalData.interface+'/xianPost/API_6041_xianGuaPaiYouLiAdd',
        method: 'POST',
        data: {
          IDCARD: this.data.USER_IDCARD,
          EMPLOYEE_ID: this.data.EMPLOYEE_ID,
          USER_ASSETS_IDCARD: this.data.USER_ASSETS_IDCARD,
          USER_ASSETS_NAME: this.data.USER_ASSETS_NAME,
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key: app.globalData.session_key,
          OPEN_UUID: app.globalData.jiami,

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {

          if (res.data.result == 'error') {
            wx.showToast({
              title: res.data.data,
              icon: 'none',
              duration: 3000
            })
          } else {
           wx.navigateTo({
            url: '../../pages/xianguapaiyouliquan/xianguapaiyouliquan'
        
           })
          }
        }
      })
    }
      setTimeout(() => {
        this.data.flag = true
      }, 5000)
    }
  },


  chaxunbaoming() {

    wx.request({
      url: app.globalData.interface+'/xianPost/API_6043_xianGuaPaiYouLiSelect',
      method: 'POST',
      data: {

        OPEN_ID: app.globalData.openid, //用户OPEN_ID
        session_key: app.globalData.session_key,
        OPEN_UUID: app.globalData.jiami,

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {

        if (res.data.result == 'error') {
          console.log('未报名');
        } else {
          wx.navigateTo({
            url: '../../pages/xianguapaiyouliquan/xianguapaiyouliquan'
          })
        }
      }
    })


  },












  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })

    setTimeout(() => {
      this.region()
      this.chaxunbaoming()
    }, 800)
 


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})